package com.axdoc.wallet.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Component;

import com.axdoc.wallet.dao.IMqRecordsDao;
import com.axdoc.wallet.entity.MqRecords;

@Component("mqRecordsDao")
public class MqRecordsDaoImpl implements IMqRecordsDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public int save(MqRecords mqr) {
		SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate);
		simpleJdbcInsert.setTableName("mq_records");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("msg_type", mqr.getMsgType());
		param.put("msg_value", mqr.getMsgValue());
		param.put("state", mqr.getState());
		param.put("msg_from", mqr.getMsgFrom());
		param.put("hash_code", mqr.getHashCode());
		param.put("unique_id", mqr.getUniqueId());
		int result = simpleJdbcInsert.execute(param);
		return result;
	}

	public int saveMqRecords(final MqRecords mqr) {
		KeyHolder keyHolder = new GeneratedKeyHolder();
		/* 插入实现 */
		final String sql = "insert into mq_records(msg_type,msg_value,state,msg_from,hash_code,unique_id) values(?,?,?,?,?,?)";
		jdbcTemplate.update(new PreparedStatementCreator() {  
            public PreparedStatement createPreparedStatement(  
                    Connection connection) throws SQLException {  
                PreparedStatement ps = connection.prepareStatement(sql,  
                        new String[] { "id" });  
                ps.setString(1, mqr.getMsgType());  
                ps.setString(2, mqr.getMsgValue()); 
                ps.setInt(3, mqr.getState()); 
                ps.setString(4, mqr.getMsgFrom()); 
                ps.setInt(5, mqr.getHashCode()); 
                ps.setString(6, mqr.getUniqueId());
                return ps;  
            }
        }
		,keyHolder);
		int id = keyHolder.getKey().intValue();
		return id;
	}

	@Override
	public boolean updateMqRecords(MqRecords mqr) {
		boolean result = false;
		/* 更新实现 */
		int i = jdbcTemplate.update(
				"update mq_records set msg_type=?,msg_value=?,hash_code=?,state=?,msg_from=?,unique_id=? where id=?",
				new Object[] {mqr.getMsgType(),mqr.getMsgValue(),mqr.getHashCode(),mqr.getState(),mqr.getMsgFrom(),mqr.getUniqueId(),mqr.getId()});
		if (i > 0) {
			result = true;
		}
		return result;
	}
	
	@Override
	public boolean updateState(int id, int state) {
		boolean result = false;
		/* 更新实现 */
		int i = jdbcTemplate.update(
				"update mq_records set state=? where id=?",
				new Object[] {state,id});
		if (i > 0) {
			result = true;
		}
		return result;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public MqRecords getMqRecord(int id) {
		List<MqRecords> list = new ArrayList<MqRecords>();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from mq_records where id = ? ");
		list = (List<MqRecords>) jdbcTemplate.query(sql.toString(), new Object[] { id }, new RowMapper() {
			@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				MqRecords pw = new MqRecords();
				pw.setId(rs.getInt("id"));
				pw.setMsgType(rs.getString("msg_type"));
				pw.setMsgValue(rs.getString("msg_value"));
				pw.setHashCode(rs.getInt("hash_code"));
				pw.setState(rs.getInt("state"));
				pw.setCreateTime(rs.getTimestamp("create_time"));
				pw.setUpdateTime(rs.getTimestamp("update_time"));
				pw.setMsgFrom(rs.getString("msg_from"));
				pw.setUniqueId(rs.getString("unique_id"));
				return pw;
			}
		});
		if(null != list && list.size()>0){
			return list.get(0);
		}
		return null;
	}
 
}
